import { baseApiUrl, path } from "../../utils/config";
import { courseInfo, courseLearn } from '../../utils/api';
import paymentRequest from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    id: null,
    poster: "",
    src: "",

    name: "",
    price: "",
    learn_number: "",
    duration: "",
    synopsis: "",
    content: "",
    btn_text: "",

    unique: [],
    users_course: null
  },
  async onLoad(options) {
    let { id } = options;
    this.setData({ id });
    this.getDeta();
  },
  async getDeta() {
    let { id } = this.data;
    let { course, users_course } = await courseInfo({ id });
    let { name, price, learn_number, duration, unique, synopsis, content } = course;
    let btn_text = price > 0 && !users_course ? "购买" : "开始开始";
    let poster = course.thumbnail;
    let src = course.video;
    content = content?.replace(/\<img/gi, '<img style="width:100%;height:auto"');
    this.setData({ id, poster, src, name, price, learn_number, duration, unique, synopsis, content, users_course, btn_text });
  },
  async submit() {
    let { id, price, users_course } = this.data;
    if (price > 0 && !users_course) {
      let { pay_data } = await courseLearn({ id });
      await paymentRequest(pay_data);
      Toast('操作成功');
      this.getDeta();
      return false;
    }
    let video = wx.createVideoContext("video");
    video.play();
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})